﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YJKArchUtils.Utils;
using YJKRevitDimensionText.OneKeyPlanDim.LevelDim.Data;
using YJKRevitDimensionText.OneKeyPlanDim.Utility;

namespace YJKRevitDimensionText.OneKeyPlanDim.LevelDim
{
	public class OutDoorFloorLevelDim
	{
		public OutDoorFloorLevelDim(Autodesk.Revit.DB.Document doc)
		{
			this.m_doc = doc;
		}

		public void AddBuildingPad(Element ele)
		{
			if (ele is TopographySurface)
			{
				this.m_lstTopgraphySurface.Add(ele as TopographySurface);
				return;
			}
			if (ele is BuildingPad)
			{
				this.m_lstBuildingPad.Add(ele as BuildingPad);
			}
		}

		public void CreateLevelDim()
		{
			foreach (BuildingPad buildingPad in this.m_lstBuildingPad)
			{
				CommonLayerFloor commonLayerFloor = new CommonLayerFloor(this.m_doc);
				List<SingleLevelDimDataInfo> dimPosition = new List<SingleLevelDimDataInfo>();
				PlanarFace planarFace = this.BuildingPadBaseFace(buildingPad);
				commonLayerFloor.dLevelVal = Common.FeetToMM(planarFace.Origin.Z) / 1000.0;
				List<List<Curve>> list = new List<List<Curve>>();
				List<Curve> xyplaneCurve = CommonInterface.GetXYPlaneCurve(GeometryHelper.GetAllEdgeList(planarFace));
				list.Add(xyplaneCurve);
				commonLayerFloor.CreateKeyComponent(ref dimPosition, list);
				commonLayerFloor.SetDimPosition(dimPosition);
			}
			foreach (TopographySurface topographySurface in this.m_lstTopgraphySurface)
			{
				CommonLayerFloor commonLayerFloor2 = new CommonLayerFloor(this.m_doc);
				List<SingleLevelDimDataInfo> dimPosition2 = new List<SingleLevelDimDataInfo>();
				List<List<Curve>> list2 = this.TopographySurfacePts(topographySurface);
				if (list2.Count != 0 && list2[0].Count != 0)
				{
					commonLayerFloor2.dLevelVal = Common.FeetToMM(list2[0][0].GetEndPoint(0).Z) / 1000.0;
					List<List<Curve>> list3 = new List<List<Curve>>();
					foreach (List<Curve> lstCv in list2)
					{
						List<Curve> xyplaneCurve2 = CommonInterface.GetXYPlaneCurve(lstCv);
						list3.Add(xyplaneCurve2);
					}
					commonLayerFloor2.CreateKeyComponent(ref dimPosition2, list2);
					commonLayerFloor2.SetDimPosition(dimPosition2);
				}
			}
		}

		private PlanarFace BuildingPadBaseFace(BuildingPad buildingPad)
		{
			foreach (PlanarFace planarFace in GeometryHelper.GetAllPlanarFace(buildingPad))
			{
				if (planarFace.FaceNormal.Normalize().IsAlmostEqualTo(XYZ.BasisZ))
				{
					return planarFace;
				}
			}
			return null;
		}

		private List<List<Curve>> TopographySurfacePts(TopographySurface topographySurface)
		{
			List<XYZ> list = topographySurface.GetBoundaryPoints().ToList<XYZ>();
			List<List<Curve>> list2 = new List<List<Curve>>();
			if (list.Count > 3)
			{
				list.Add(list[0]);
			}
			List<Curve> list3 = new List<Curve>();
			for (int i = 0; i < list.Count - 1; i++)
			{
				Curve curve = this.CreateCvBy2Pts(list[i], list[i + 1]);
				if (curve != null)
				{
					list3.Add(curve);
				}
			}
			list2.Add(list3);
			return list2;
		}

		private Curve CreateCvBy2Pts(XYZ pt1, XYZ pt2)
		{
			Line result = null;
			try
			{
				result = Line.CreateBound(pt1, pt2);
			}
			catch
			{
			}
			return result;
		}

		private Document m_doc;

		private List<TopographySurface> m_lstTopgraphySurface = new List<TopographySurface>();

		private List<BuildingPad> m_lstBuildingPad = new List<BuildingPad>();
	}
}
